# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import time

from openerp.osv import fields, osv
from openerp.tools.translate import _

class change_line_rate(osv.osv_memory):
    _name = 'change.line.rate'
    _description = 'Change Line Rate'

    def change_rate_test(self, cr, uid, ids, context=None):
#         raise osv.except_osv(_('Error!'), _('Found more than one currency at the payment lines.'))
        av_line_obj = self.pool.get('account.voucher.line')
        av_obj = self.pool.get('account.voucher')
        mod_obj = self.pool.get('ir.model.data')
        change_rate = 0
        type = False
        av_id = False
        for obj in self.browse(cr, uid, ids, context=context):
            change_rate = obj.change_rate
        for av in av_obj.browse(cr, uid, context.get(('active_ids'), []), context=context):
            av_id = av.id
            type = av.type
            currency = False
            if av.line_ids:
                for ln in av.line_ids:
                    if currency:
                        if currency != ln.currency_id.id:
                            raise osv.except_osv(_('Error!'), _('Found more than one currency at the payment lines.'))
                    else:
                        currency = ln.currency_id.id
                    av_line_obj.write(cr, uid, ln.id, {'change_rate':change_rate}, context=context)
                    if ln.reconcile:
                        vals = av_line_obj.get_balance(cr, uid, av.journal_id.id, av.date, av.change_rate, ln.move_line_id.id, ln.currency_id.id, change_rate, ln.reconcile, ln.amount, context)
                        av_line_obj.write(cr, uid, ln.id, {'amount':vals['amount_unreconciled']}, context=context)
        if type == 'payment':
            dummy, action_id = tuple(mod_obj.get_object_reference(cr, uid, 'account_voucher', 'action_vendor_payment'))
            action = self.pool.get('ir.actions.act_window').read(cr, uid, action_id, context=context)
            res = mod_obj.get_object_reference(cr, uid, 'account_voucher', 'view_vendor_payment_form')
            action['views'] = [(res and res[1] or False, 'form')]
            action['res_id'] = av_id or False
        if type == 'receipt':
            dummy, action_id = tuple(mod_obj.get_object_reference(cr, uid, 'account_voucher', 'view_vendor_receipt_form'))
            action = self.pool.get('ir.actions.act_window').read(cr, uid, action_id, context=context)
            res = mod_obj.get_object_reference(cr, uid, 'account_voucher', 'action_vendor_receipt')
            action['views'] = [(res and res[1] or False, 'form')]
            action['res_id'] = av_id or False
        return action



    _columns = {
        'change_rate': fields.float('Change Rate', digits=(12,6), help='Change Currency Rate'),
    }

change_line_rate()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
